package util;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;


import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfPrint
	{
		private String title;
		private String subject;
		private String keywords;
		private String maker;
		private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
			      Font.BOLD);
			  private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
			      Font.NORMAL, BaseColor.RED);
			  private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
			      Font.BOLD);
			  private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
			      Font.BOLD);
			  
		public PdfPrint(String loc,String title, String subject, String keywords, String maker, int infoType, String cityColumns[], ArrayList<String> cityName, ArrayList<String> cityLoc, ArrayList<String> countryName, ArrayList<String> countryTld )
			{
				
				this.title = title;
				this.subject = subject;
				this.keywords = keywords;
				this.maker = maker;
				Document document = new Document();
				try
					{
						PdfWriter.getInstance(document, new FileOutputStream(loc));
						document.open();
						addMeta(document, title, subject, keywords, maker);
						addFrontPage(document, title, maker);
						addContent(document, infoType, cityColumns, cityName, cityLoc, countryName, countryTld);
						
						document.close();
					} catch (Exception e)
					{
						e.printStackTrace();
					} 
			}
		private static  void addMeta(Document document, String title, String subject, String keywords, String maker )
			{
				document.addTitle(title);
				document.addSubject(subject);
				document.addKeywords(keywords);
				document.addAuthor(maker);
				document.addCreator(maker);
			}
		
		private static void addFrontPage(Document document, String title, String maker) throws DocumentException
			{
				Paragraph preface = new Paragraph();
				
				addEmptyLine(preface, 1);
				
				preface.add(new Paragraph(title, catFont));
				
				addEmptyLine(preface, 1);
				
				preface.add(new Paragraph("Report generated by: " + maker + " on " + new Date(), smallBold));
				
				addEmptyLine(preface, 30);
				
				preface.add(new Paragraph("This document is a preliminary version and should not be taken as the finished product", redFont));
				
				document.add(preface);
				
				document.newPage();
				
				
			}
		
		private static void addContent(Document document, int infoType, String cityCountryColumns[], ArrayList<String> cityName, ArrayList<String> cityLoc, ArrayList<String> countryName, ArrayList<String> countryTld) throws DocumentException
		{
			switch (infoType) {
			case 1:
				Anchor anchor = new Anchor("Cities", catFont);
				anchor.setName("Cities");
				
				Chapter catPart = new Chapter(new Paragraph(anchor), 1);
				
				Paragraph subPara = new Paragraph();
				Section subPart = catPart.addSection(subPara);
				
				createCitiesTable(subPart, cityCountryColumns, cityName, cityLoc);
				
				
				document.add(catPart);
				break;
				
				case 2:
					Anchor anchor2 = new Anchor("Cities", catFont);
					anchor2.setName("Cities");
					
					Chapter catPart2 = new Chapter(new Paragraph(anchor2), 1);
					
					Paragraph subPara2 = new Paragraph();
					Section subPart2 = catPart2.addSection(subPara2);
					
					createCountryTable(subPart2, cityCountryColumns, countryName, countryTld);

					document.add(catPart2);
					break;
				/*	
				case 3:
					if(backCountry == null)
						backCountry = new CountryUI();
					backCountry.display();
					break;
				case 4:
					if(backCustomer == null)
						backCustomer = new CustomerUI();
					backCustomer.display();
					break;
				case 5:
					if(backSupplier == null)
						backSupplier = new SupplierUI();
					backSupplier.display();
					break;
				case 6:
					if(backAbout == null)
						backAbout = new AboutUI();
					backAbout.display();
					break;
				case 7:
					if(backEmployee == null)
						backEmployee = new EmployeeUI();
					backEmployee.display();
					break;
				case 8:
					if(backSubcontractor == null)
						backSubcontractor = new SubContractorUI();
					backSubcontractor.display();
					break;
				case 9:
					if(backItem == null)
						backItem = new ItemUI();
					backItem.display();
					break;
				case 10:
					if(backOffer == null)
						backOffer = new OfferUI();
					backOffer.display();
					break;
				case 11:
					if(backProject == null)
						backProject = new ProjectUI();
					backProject.display();
					break;
				case 12:
					if(backRequest == null)
						backRequest = new RequestsUI();
					backRequest.display();
					break;
				case 13:
					if(backStatistic == null)
						backStatistic = new StatisticsUI();
					backStatistic.display();
					break;
					*/
			}
				
			
		}

		
		private static void addEmptyLine(Paragraph paragraph, int number)
			{
				for (int i = 0; i < number ; i++)
				{
					paragraph.add(new Paragraph(" "));
				}
			}
		
		private static void createCitiesTable(Section section, String columns[], ArrayList<String> listName, ArrayList<String> listCountry)
		throws BadElementException
		{
			PdfPTable table = new PdfPTable(2);
			
			PdfPCell c1;
			
			for(String obj : columns)
				{
					c1 = new PdfPCell(new Phrase(obj));
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.addCell(c1);
				}
			table.setHeaderRows(1);
			
			for(int i = 0; i <listName.size(); i++)
				{
					table.addCell(listName.get(i));
					table.addCell(listCountry.get(i));
				}
			section.add(table);
			
		}
		private static void createCountryTable(Section section, String columns[], ArrayList<String> listName, ArrayList<String> listTld)
				throws BadElementException
				{
					PdfPTable table = new PdfPTable(2);
					
					PdfPCell c1;
					
					for(String obj : columns)
						{
							c1 = new PdfPCell(new Phrase(obj));
							c1.setHorizontalAlignment(Element.ALIGN_CENTER);
							table.addCell(c1);
						}
					table.setHeaderRows(1);
					
					for(int i = 0; i <listName.size(); i++)
						{
							table.addCell(listName.get(i));
							table.addCell(listTld.get(i));
						}
					section.add(table);
					
				}
		
		
	}
